package com.example.commons.aspect;

import com.example.commons.annotation.DebounceLock;
import com.example.commons.annotation.SpendTime;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Author:汪彬彬
 * date:2024/11/15 12:00
 */
@Slf4j
@Aspect
@Component
@Order(2)
public class SpendTimeAspect {

    @Around("@annotation(com.example.commons.annotation.SpendTime)")
    public Object debounceLock(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        SpendTime requestLock = method.getAnnotation(SpendTime.class);
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long  end = System.currentTimeMillis();
         if(requestLock!=null){
             log.info("调接口用时{}ms",end-start);
         }
        return proceed;
    }
}
